#!/usr/bin/env bash

set -e
set -u

NPROCESSORS=`nproc`

source "$(dirname $0)/build-dev-bundle-common.sh"
echo CHECKOUT DIR IS "$CHECKOUT_DIR"
MONGO_VERSION=$MONGO_VERSION_64BIT
if [ $ARCH = "i686" ]; then
  MONGO_VERSION=$MONGO_VERSION_32BIT
fi
if [ $ARCH = "armv6l" -o $ARCH = "armv7l" ] ; then
  MONGO_VERSION=$MONGO_VERSION_ARMV7l
fi
echo BUILDING MONGO "v$MONGO_VERSION" IN "$DIR"

echo "Please remove the old version openssl in your system first!"
echo "Please install the build enviroment"
echo "We assume you have done this yet!"
echo "To do that in debian or ubuntu, usually do likely this,"
echo "sudo apt remove openssl && sudo apt remove libssl-dev"
echo "sudo apt update"
echo "sudo apt install build-essential checkinstall zlib1g-dev -y"
# We want to build a binary that includes SSL support but does not depend on a
# particular version of openssl on the host system.
OPENSSL="OpenSSL_1_0_2o"
OPENSSL_URL="https://github.com/openssl/openssl/archive/$OPENSSL.tar.gz"
curl -L $OPENSSL_URL | tar zx

cd "openssl-$OPENSSL"
if [ "$UNAME" == "Linux" ]; then
  if [ $ARCH = "armv6l" -o $ARCH = "armv7l" ] ; then
    ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
  else
    ./config --prefix="$DIR/build/openssl-out" no-shared
  fi
else
  # This configuration line is taken from Homebrew formula:
  # https://github.com/mxcl/homebrew/blob/master/Library/Formula/openssl.rb
  ./Configure no-shared zlib-dynamic --prefix="$DIR/build/openssl-out" darwin64-x86_64-cc enable-ec_nistp_64_gcc_128
fi
if [ $ARCH = "armv6l" -o $ARCH = "armv7l" ] ; then
  make
  sudo bash -c "make install"
  sudo bash -c "echo /usr/local/ssl/lib > /etc/ld.so.conf.d/$OPENSSL.conf"
  sudo bash -c "ldconfig -v"
  sudo bash -c "echo 'PATH=/usr/local/ssl/bin:\$PATH' > /etc/profile.d/openssl.sh"
  source /etc/profile.d/openssl.sh
  sudo bash -c "rm -rf /usr/local/include/openssl"
  sudo bash -c "ln -s /usr/local/ssl/include/openssl /usr/local/include/openssl"
else
  make install
fi
# To see the mongo changelog, go to http://www.mongodb.org/downloads,
# click 'changelog' under the current version, then 'release notes' in
# the upper right.
cd "$DIR/build"

# Download and build mongodb. Only support version from 3.2 to 3.3
MONGO_NAME="mongo-r${MONGO_VERSION}"
if [ $UNAME = "Linux" ] ; then
  MONGO_URL="https://github.com/mongodb/mongo/archive/r$MONGO_VERSION.tar.gz"
  echo "Download from $MONGO_URL"
  curl -L $MONGO_URL | tar zx
else
  echo "$UNAME OS is not supported yet!"
fi


# Compile

# check number of cores for parallelism flag
if [ "$NPROCESSORS" -lt "4" ] ; then
    MONGO_FLAGS="--ssl --release -j1 MONGO_VERSION=$MONGO_VERSION " 
else
    MONGO_FLAGS="--ssl --release -j4 MONGO_VERSION=$MONGO_VERSION "
fi

if [ $ARCH = "armv6l" -o $ARCH = "armv7l" ] ; then
    MONGO_FLAGS+=" --disable-warnings-as-errors  CPPPATH=/usr/local/ssl/include/openssl LIBPATH=/usr/local/ssl/lib --wiredtiger=off --mmapv1=on"
else
    MONGO_FLAGS+="--cpppath=$DIR/build/openssl-out/include --libpath=$DIR/build/openssl-out/lib "
fi

if [ "$OS" == "osx" ]; then
    # NOTE: '--64' option breaks the compilation, even it is on by default on x64 mac: https://jira.mongodb.org/browse/SERVER-5575
    MONGO_FLAGS+="--openssl=$DIR/build/openssl-out/lib "
    cd $MONGO_NAME
    echo "MONGO_FLAGS:$MONGO_FLAGS"
    buildscripts/scons.py $MONGO_FLAGS mongo mongod
elif [ "$OS" == "linux" ]; then
    if [ $ARCH = "armv6l" -o $ARCH = "armv7l" ] ; then
	TEMP_PATH=`pwd`
        cd $MONGO_NAME/src/third_party/mozjs-38/
	bash ./get_sources.sh
	bash ./gen-config.sh arm linux
	cd $TEMP_PATH
    else
        MONGO_FLAGS+="--no-glibc-check "
    fi
    if [ "$ARCH" == "x86_64" ]; then
      MONGO_FLAGS+="--64"
    fi
    cd $MONGO_NAME
    echo "MONGO_FLAGS:$MONGO_FLAGS"
    buildscripts/scons.py $MONGO_FLAGS mongo mongod
else
    echo "We don't know how to compile mongo for this platform"
    exit 1
fi

echo "Done with scons build"

# Copy binaries
MONGO_NAME="mongodb-${OS}-${ARCH}-${MONGO_VERSION}"
mkdir -p "$DIR/$MONGO_NAME/bin"
cp mongo "$DIR/$MONGO_NAME/bin/"
cp mongod "$DIR/$MONGO_NAME/bin/"

# Copy mongodb distribution information
find ./distsrc -maxdepth 1 -type f -exec cp '{}' ../mongodb \;

cd "$DIR"
stripBinary $MONGO_NAME/bin/mongo
stripBinary $MONGO_NAME/bin/mongod

echo BUNDLING

cd "$DIR"
rm -rf build
tar czvf "${CHECKOUT_DIR}/$MONGO_NAME.tar.gz" .

echo DONE
